﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.IO;
using System.Net;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";

        
        
        int useridx = MediaUtil.ReturnMemberId();
        string userid = MediaUtil.ReturnUserID();
        
        // useridx = 21964264;
        // userid = "flowx";
        
        string action = string.Empty;
        string output = string.Empty;
        if (context.Request["action"] != null && context.Request["action"].Length > 0)
        {
            action = context.Request["action"].ToString();
        }
        else
        {
            return;
        }
        DateTime now = System.DateTime.Now;
        DateTime n1 = Convert.ToDateTime("2013-05-10");
        DateTime n2 = Convert.ToDateTime("2013-05-12 23:59:59");

        //if (now < n1 || now > n2)
        //{
        //    return; 
        //}
        
        if (action == "rank")
        {
            //if (now < n1)
            //{
            //    output = "";
            //}
            //else
            //{
                output = getRank1();
            //}
        }
        if (action == "rank2")
        {
            //if (now < n1)
            //{
            //    output = "";
            //}
            //else
            //{
                output = getRank2();
           //}
        }
        if (action == "user")
        {
            int allflowernum = getStarflowerNum(useridx);
            List<string> lst=new List<string>();
            lst = getUserInfo(useridx);
            int seed = Convert.ToInt32(lst[0].ToString());
            if (seed < 1) seed = 0;
            int num1 = Convert.ToInt32(lst[1].ToString());
            int lotterynum = allflowernum / 100 - num1;
            if (lotterynum < 1) lotterynum = 0;
            
            ////测试
            //if (useridx == 21964264)
            //{
            //    lotterynum=1;
            //}
            ////end
            
            
            string res = "[{\"seed\":" + seed + ",\"lnum\":" + lotterynum + "}]";
            output = res;
            
            
        }
        if (action == "seed")
        {
            int seednum = getseed(useridx, userid);
            SendGift(999, useridx);
            output = seednum.ToString();
        }
        if (action == "win")
        {
            int res = lottery(useridx, userid);  //中奖项
            SendGift(res, useridx);
            string msg = "";
            if (res == 1)
            {
                msg = " 恭喜您获得夜来香种子1枚";
            }
            else if (res == 2 || res == 21)
            {
                msg = "恭喜您获得水仙种子1枚";
            }
            else if (res == 3)
            {
                msg = "恭喜您获得百合花种子1枚";
            }
            else if (res == 4)
            {
                msg = "恭喜您获得马蹄莲种子1枚";
            }
            else if (res == 5)
            {
                msg = "恭喜您获得薰衣草种子1枚";
            }
            else if (res == 6)
            {
                msg = "恭喜您获得睡莲种子1枚";
            }
            else if (res == 7 || res == 10)
            {
                msg = "恭喜您获得含笑种子1枚";
            }
            else if (res == 8 || res == 11 || res == 20)
            {
                msg = "恭喜您获得金币500";
            }
            else if (res == 9)
            {
                msg = "恭喜您获得绿花瓷1个";
            }
            else if (res == 12)
            {
                msg = "恭喜您获得玫瑰种子1枚";
            }
            else if (res == 13 || res == 16)
            {
                msg = "恭喜您获得金币5000";
            }
            else if (res == 14 || res == 17)
            {
                msg = "恭喜您获得花肥1包";
            }
            else if (res == 15 || res == 18)
            {
                msg = "恭喜您获得金币10W";
            }
            else if (res == 19)
            {
                msg = "恭喜您获得蓝花瓷1个";
            }
            else if (res == 22)
            {
                msg = "恭喜您获得青花瓷1个";
            }

            output = "[{\"ret\":" + res.ToString() + ",\"msg\":\""+msg+"\"}]";
        }

        context.Response.Write(output);
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    /// <summary>
    /// 星星花排行榜
    /// </summary>
    /// <returns></returns>
    public string getRank1()
    {
        string str = "";
        if (HttpRuntime.Cache["active_flowerkingRank1"] == null)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://122.226.86.96:8080/gardenService/GetStarFlower/doGet.do");
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            str = sr.ReadToEnd().Replace(" ", "").Trim();
        }
        else
        {
            str = HttpRuntime.Cache["active_flowerkingRank1"].ToString();
        }
        return str;
    }
    /// <summary>
    /// 花店排行榜
    /// </summary>
    /// <returns></returns>
    public string getRank2()
    {
        string str = "";
        if (HttpRuntime.Cache["active_flowerkingRank2"] == null)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://122.226.86.96:8080/gardenService/GetEstimateRank/doGet.do");
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
             str = sr.ReadToEnd().Replace(" ", "").Trim();
        }
        else
        {
            str = HttpRuntime.Cache["active_flowerkingRank2"].ToString();
        }
        return str;
    }
    
    /// <summary>
    /// 抽奖
    /// </summary>
    /// <param name="useridx"></param>
    /// <param name="userid"></param>
    /// <returns>中奖项</returns>
    public static int lottery(int useridx,string userid)
    {
        int ret = 0;
        
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,2),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,50,userid),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)                  
                    };
        try
        {
            SqlHelper.ExecuteNonQuery(active.Conn, CommandType.StoredProcedure, "[pr_FlowerKing2013]", p);
            ret = int.Parse(p[3].Value.ToString());
        }
        catch (Exception ex)
        {
            string ss = ex.Message.ToString();
        }
        return ret;
    }
    
    /// <summary>
    /// 领取种子
    /// </summary>
    /// <param name="useridx"></param>
    /// <param name="userid"></param>
    /// <returns>返回可领种子数</returns>
    public static int getseed(int useridx, string userid)
    {
        int ret = 0;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@type",SqlDbType.Int,4,1),
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,50,userid),
                         SqlHelper.MakeOutParam("@ret",SqlDbType.Int,4)                  
                    };
        try
        {
            SqlHelper.ExecuteNonQuery(active.Conn, CommandType.StoredProcedure, "[pr_FlowerKing2013]", p);
            ret = int.Parse(p[3].Value.ToString());
        }
        catch(Exception ex)
        {
            string ss = ex.Message.ToString();
        }
        if (ret < 0) ret = 0;
        return ret;
    }
   
    /// <summary>
    /// 发送礼物 花王接口
    /// </summary>
    /// <param name="res">中奖项</param>
    public static void SendGift(int res,int useridx) 
    {
        string url = "";
        if (res == 1) //夜来香
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=8";
        }
        else if (res == 2 || res==21)//水仙
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=17";
        }
        else if (res == 3)//百合
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=23";
        }
        else if (res == 4)//马蹄
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=21";
        }
        else if (res == 5)//熏衣
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=14";
        }
        else if (res == 6)//睡莲
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=30";
        }
        else if ((res == 7) || (res==10))//含笑
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=10";
        }
        else if (res == 9)//绿花瓷
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=2&publicid=10";
        }
        else if (res == 12)//玫瑰
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=29";
        }
        else if (res == 14|| res==17 ) //花肥
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=2&publicid=1";
        }
        else if (res == 19)//蓝花瓷
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=2&publicid=5";
        }
        else if (res == 22)//青花瓷
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=2&publicid=7";
        }
        else if (res == 999)//星星花
        {
            url = "http://122.226.86.96:8080/gardenService/SetReward/doGet.do?userid=" + useridx + "&count=1&emtype=1&publicid=61";
        }

        if (url != "")
        {

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default);
            string str = sr.ReadToEnd().Replace(" ", "").Trim();
        }


    }
    /// <summary>
    /// 获取用户赠送星星花总数
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public int getStarflowerNum(int useridx)
    {
        int res = 0;
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://122.226.86.96:8080/gardenService/GetSendCount/doGet.do?userid=" + useridx);
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            Stream stream = webResponse.GetResponseStream();
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            string str = sr.ReadToEnd().Replace(" ", "").Trim();

            string[] lst=str.Split(':');
            string val = lst[1].Replace("\"", "").Replace("}]","");
            res = Convert.ToInt32(val.Trim());    
            
            
            
        }
        catch (Exception ex)
        {
            string sss = ex.Message.ToString();
            res = 0; 
        }
        return res;
    }


    public List<string> getUserInfo(int useridx)
    {
        List<string> lst = new List<string>();
        int seednum = 0;
        int lotterynum = 0;
        SqlParameter[] p =
                    {
                         SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                         SqlHelper.MakeOutParam("@seed",SqlDbType.Int,4), 
                         SqlHelper.MakeOutParam("@lotterycount",SqlDbType.Int,4)                  
                    };
        try
        {
            SqlHelper.ExecuteNonQuery(active.Conn, CommandType.StoredProcedure, "[active_UserInfo_flowerking]", p);
            seednum = int.Parse(p[1].Value.ToString());
            lst.Add(seednum.ToString());
            lotterynum = int.Parse(p[2].Value.ToString());
            lst.Add(lotterynum.ToString());
            
        }
        catch (Exception ex)
        {
            string ss = ex.Message.ToString();
            lst.Add("0");
            lst.Add("0");
        }
        
        return lst;
    } 
    

}